package game.module.dabt.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.common.ItemConstants;
import game.entity.PlayingRole;
import game.module.dabt.bean.DabtBean;
import game.module.dabt.dao.DabtCache;
import game.module.dabt.dao.DabtdbDao;
import game.module.item.bean.GainItemBean;
import game.module.item.logic.ItemManager;
import game.module.mail.logic.MailManager;
import game.module.user.bean.PlayerBean;
import game.module.user.dao.PlayerDao;
import game.session.SessionManager;

public class PayMoneyBack {
	private static Logger logger = LoggerFactory.getLogger(PayMoneyBack.class);

	static class SingletonHolder {
		static PayMoneyBack instance = new PayMoneyBack();
	}

	public static PayMoneyBack getInstance() {
		return SingletonHolder.instance;
	}

	public void PaymoneybackOutlineAndaddJindou(int playerId,long jindouNum) {
		HallServer.executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				long newnum = jindouNum;
				List<DabtBean> datas = DabtdbDao.getInstance().getDabtBeanById(playerId);
				if (datas.size() > 0) {
					datas.sort((DabtBean da1, DabtBean da2) -> da2.getDabt_money().compareTo(da1.getDabt_money()));
					for (int i = 0; i < datas.size(); i++) {
						if (datas.get(i).getDabt_money() <= 0)
							continue;
						if (newnum >= datas.get(i).getDabt_money()) {// 这条记录被还清
							newnum -= datas.get(i).getDabt_money();
							datas.get(i).setDabt_money(0);
							DabtdbDao.getInstance().updateDabtIsPay(playerId, true);
							sendPayAllMoneyMail(playerId,(int)jindouNum,datas.get(i).getCreditor_id(),datas.get(i).getDabt_money());
							sendTakeAllMoneyMail(datas.get(i).getCreditor_id(),playerId,datas.get(i).getDabt_money());
//							MailManager.getInstance().sendPayAllMoneyMail(playerId, (int) jindouNum,
//									datas.get(i).getCreditor_id(), datas.get(i).getDabt_money());
//							MailManager.getInstance().sendTakeAllMoneyMail(datas.get(i).getCreditor_id(), playerId,
//									datas.get(i).getDabt_money());
							// 发邮件改数据

						} else {
							sendPayPartMoneyMail(playerId,(int)newnum,datas.get(i).getCreditor_id(),datas.get(i).getDabt_money() - (int) newnum);
							sendTakePartMoneyMail(datas.get(i).getCreditor_id(),(int)newnum,playerId,datas.get(i).getDabt_money() - (int) newnum);
							// 本条记录没有被还清减去一部分钱
//							MailManager.getInstance().sendTakePartMoneyMail(datas.get(1).getCreditor_id(), (int) newnum,
//									playerId, datas.get(i).getDabt_money() - (int) newnum);
//							MailManager.getInstance().sendPayPartMoneyMail(playerId, (int) newnum,
//									datas.get(1).getCreditor_id(), datas.get(i).getDabt_money() - (int) newnum);
							DabtdbDao.getInstance().updateDabtNum(playerId, (int) newnum);
							// 发邮件该数据
							break;
						}

					}
					if (newnum > 0)
						PlayerDao.getInstance().addJinDou(jindouNum, playerId);
				} else {
					// 没有欠钱
					PlayerDao.getInstance().addJinDou(jindouNum, playerId);
				}
			}
		});
		
		
	}
	
	public long payMoneybackonline(int playerId, boolean onlion, long jindouNum) {
		long newnum = jindouNum;
		if(jindouNum<0)
			return jindouNum;
		PlayingRole pr = SessionManager.getInstance().getPlayer(playerId);
		List<DabtBean> datas = DabtCache.getInstance().getDabt(playerId);
		if(datas==null)
			return jindouNum;
		if (datas.size() > 0) {
			datas.sort((DabtBean da1, DabtBean da2) -> da2.getDabt_money().compareTo(da1.getDabt_money()));// 排序
			for (int i = 0; i < datas.size(); i++) {
				if (datas.get(i).getDabt_money() <= 0)
					continue;
				if (newnum >= datas.get(i).getDabt_money()) {
					newnum -= datas.get(i).getDabt_money();
					
					DabtDbHelper.getInstance().asyncUpdateDabtIsPay(playerId);
					sendPayAllMoneyMail(playerId,(int)jindouNum,datas.get(i).getCreditor_id(),datas.get(i).getDabt_money());
					sendTakeAllMoneyMail(datas.get(i).getCreditor_id(),playerId,datas.get(i).getDabt_money());
					datas.get(i).setDabt_money(0);
				} else {
					// 本条记录没有被还清减去一部分钱
					
					sendPayPartMoneyMail(playerId,(int)newnum,datas.get(i).getCreditor_id(),datas.get(i).getDabt_money() - (int) newnum);
					sendTakePartMoneyMail(datas.get(i).getCreditor_id(),(int)newnum,playerId,datas.get(i).getDabt_money() - (int) newnum);
					datas.get(i).setDabt_money(datas.get(i).getDabt_money() - (int) newnum);
					DabtDbHelper.getInstance().asyncupdateDabtNum(playerId,(int)newnum);// 异步处理
					newnum = 0;
					// 发邮件该数据
					break;
				}
			}
			return newnum;
		} else {
			// 没有欠钱
			return jindouNum;
		}
	}
	private void sendTakePartMoneyMail(int creditorPlayerId,int paymoner,int playerId,int remaindabt) {
		Map<String, String> params = new HashMap<String, String>();
		PlayingRole pr = SessionManager.getInstance().getPlayer(playerId);
		if (pr != null) {
			params.put("$1", pr.getPlayerBean().getName());
		} else {
			PlayerBean pd = PlayerDao.getInstance().getPlayerById(playerId);
			params.put("$1", pd.getName());
		}
		params.put("$2", "" + playerId);
		params.put("$3", ""+paymoner);
		params.put("$4",""+remaindabt);
		List<GainItemBean> item = new ArrayList<GainItemBean>();
		GainItemBean aItem = new GainItemBean();
		aItem.setItemTemplateId(ItemConstants.YUANBAO_ITEM_ID);
		aItem.setCount(paymoner);
		item.add(aItem);
		MailManager.getInstance().sendMailSigleToPlayer(creditorPlayerId, MailManager.GET_PART_PAY_INFORM, params, item);
		
	}
	private void sendPayPartMoneyMail(int playerId,int paymoney,int creditorId,int remiandabt){
		logger.info("payback part  ={} ",playerId);
		Map<String, String> params = new HashMap<String, String>();
		params.put("$1", paymoney+"");
		PlayingRole pr = SessionManager.getInstance().getPlayer(creditorId);
		if (pr != null) {
			params.put("$2", pr.getPlayerBean().getName());
		} else {
			PlayerBean pd = PlayerDao.getInstance().getPlayerById(creditorId);
			params.put("$2", pd.getName());
		}
		params.put("$3", "" + paymoney);
		params.put("$4", ""+remiandabt);
		MailManager.getInstance().sendMailSigleToPlayer(playerId, MailManager.PAY_PART_INFORM, params, null);
	}
	private void sendPayAllMoneyMail(int playerId,int alljindou,int creditor_id,int payback) {
		logger.info("payback all ={} ",playerId);
		Map<String, String> params = new HashMap<String, String>();
		params.put("$1", alljindou+"");
		PlayingRole pr = SessionManager.getInstance().getPlayer(creditor_id);
		if (pr != null) {
			params.put("$2", pr.getPlayerBean().getName());
		} else {
			PlayerBean pd = PlayerDao.getInstance().getPlayerById(creditor_id);
			params.put("$2", pd.getName());
		}
		params.put("$3", "" + payback);
		params.put("$4", ""+(alljindou-payback));
		MailManager.getInstance().sendMailSigleToPlayer(playerId, MailManager.PAY_BACK_INFORM, params, null);
	}
	
	private void sendTakeAllMoneyMail(int toCreditor_Player,int debtPlayer,int payback) {
		//logger.info("pay all dabt money ={} ",playerId);
		Map<String, String> params = new HashMap<String, String>();
		PlayingRole pr = SessionManager.getInstance().getPlayer(debtPlayer);
		if (pr != null) {
			params.put("$1", pr.getPlayerBean().getName());
		} else {
			PlayerBean pd = PlayerDao.getInstance().getPlayerById(debtPlayer);
			params.put("$1", pd.getName());
		}
		params.put("$2", "" + debtPlayer);
		params.put("$3",  ""+payback);
		List<GainItemBean> item = new ArrayList<GainItemBean>();
		GainItemBean aItem = new GainItemBean();
		aItem.setItemTemplateId(ItemConstants.YUANBAO_ITEM_ID);
		aItem.setCount(payback);
		item.add(aItem);
		MailManager.getInstance().sendMailSigleToPlayer(toCreditor_Player, MailManager.GET_PAY_INFORM, params, item);
	}
}
